
package MyCalculator;

public class BackEndCalculator {

    BackEndCalculator() {
    }

    public double singleInputCalculation(double inputNum, int CalType) {
        double outputresult = 0;
        double startingnumber = 0;

        switch (CalType) {
            case 1: {


                outputresult = Math.sin(inputNum);

                break;
            }
            case 2: {
                outputresult = Math.cos(inputNum);

                break;
            }

            case 3: {
                outputresult = Math.tan(inputNum);
                break;
            }

            case 4: {

                outputresult = Math.asin(inputNum);
                break;
            }


            case 5: {
                outputresult = Math.acos(inputNum);
                break;
            }



            case 6: {
                outputresult = Math.atan(inputNum);

                break;
            }

            case 7: {
                outputresult = Math.log10(inputNum);

                break;

            }

            case 8: {

                outputresult = Math.log(inputNum);

                break;
            }


            case 9: {

                outputresult = Math.log(inputNum) / (Math.log(2));
                break;
            }

            case 10: {


                switch ((int) inputNum) {
                    case 0: {
                        outputresult = 1;
                        break;
                    }
                    case 1: {
                        outputresult = 1;
                        break;
                    }

                    default: {

                        startingnumber = inputNum;
                        while (inputNum > 1) {

                            inputNum--;
                            startingnumber = startingnumber * inputNum;
                        }
                    }
                }
                 outputresult = startingnumber;
                 break;
            }



            case 11: {
                outputresult = Math.sqrt(inputNum);

                break;
            }

            case 12: {

                outputresult = inputNum * (-1);


                break;
            }


            case 13: {
                outputresult = 1 / inputNum;

                break;
            }

            default: {
                  System.err.println("Invalid CalType");
            }
     }


        return outputresult;
    }

    public double TwoInputsCalculation(double inputNum1, double inputNum2, int CalType) {

        double outputresult = 0;
        switch (CalType) {

            case 14: {
                outputresult = inputNum1 + inputNum2;

                break;
            }

            case 15: {

                outputresult = inputNum1 - inputNum2;
                break;
            }

            case 16: {
                outputresult = inputNum1 * inputNum2;
                break;
            }


            case 17: {
                outputresult = inputNum1/inputNum2;
                break;
            }


            case 19: {


                outputresult = Math.pow(inputNum1, inputNum2);
                break;
            }


            case 20: {
                outputresult = inputNum1 % inputNum2;
                break;
            }

        }
             return outputresult;

    }

    public double MultipleInputsCalculation(double inputNum[], int CalType) {
        double outputresult = 0;
        double mean = 0;
        double newsum=0;
        double sumx = 0;
        int i = 0;

        double total = inputNum.length;

        switch (CalType) {
            case 21: {
                newsum=this.CalcSum(inputNum);

                mean = newsum / total;
                for (i = 0; i < inputNum.length; i++) {
                    sumx += Math.pow((inputNum[i] - mean), 2);
                }
                outputresult = Math.sqrt(sumx / total);
                break;
            }

            case 22: {
               newsum=this.CalcSum(inputNum);
               outputresult = newsum / total;
                break;

            }

            case 23: {

                double[] sortedArray = this.sortArry(inputNum);
                int remainder = 0;
                remainder = (sortedArray.length) / 2;

                outputresult = sortedArray[remainder];
                break;



            }
            case 24: {
                double[] sortedArray = this.sortArry(inputNum);
                outputresult = sortedArray[sortedArray.length - 1] - sortedArray[0];
                break;

            }

            case 25: {
                double[] sortedArray = this.sortArry(inputNum);
                outputresult = sortedArray[sortedArray.length - 1];
                break;


            }
            case 26: {
                double[] sortedArray = this.sortArry(inputNum);
                outputresult = sortedArray[0];
                break;

            }

            case 27: {
                newsum=this.CalcSum(inputNum);
                outputresult = newsum;
                break;

            }
            case 28: {
                int max = 0;
                int counter =1;
                int modenumber = 0;
                int ii = 1;
                double[] sortedArray = this.sortArry(inputNum);



                double Element=sortedArray[0];
                while (ii < sortedArray.length) {

                    if (Element == sortedArray[ii]) {
                        counter++;
                        if (ii == sortedArray.length - 1) {
                            if (counter > max) {

                                max = counter;
                                modenumber = ii - 1;
                            }

                        }
                    }
                    else {
                        Element=sortedArray[ii];
                        if (counter > max) {

                            max = counter;
                            modenumber = ii - 1;
                        }
                        counter = 1;
                    }
                    ii++;
                }

                outputresult = sortedArray[modenumber];

        }



        } return outputresult;}

    private double[] sortArry(double[] in) {


        int n = in.length;
        for (int pass = 1; pass < n; pass++) {
            for (int i = 0; i < n - pass; i++) {
                if (in[i] > in[i + 1]) {
                    double temp = in[i];
                    in[i] = in[i + 1];
                    in[i + 1] = temp;
                }

            }
        }
        return in;

    }

    private double CalcSum(double[] in){
       int i=0;
      double sum=0;
        while (i < in.length) {
                    sum += in[i];
                    i++;
                }
      return sum;

    }
    }







